jQuery(document).ready(function($) {
    $.ajaxSetup({cache: false});
    $('.icon-painel-online-user').popover({
        html: true,
        trigger: 'hover'
    });


    //$('.clock-count-down').each(applyCountData);
    $('.clock-count-down').onTickerTime(
            {
                triggerdate: 'data-count',
                tickerInterval: 0.3,
                DisplayFormat: "%%D%% %%H%%:%%M%%:%%S%%",
                update: function(element, secs) {
                    var id = $(element).attr('data-lt-id'),
                            tickers = $.onTickerleilao.tickers, dafault_date = $(element).data('TargetDate'),
                            tkstamp = $.onTickerleilao.tickers_stamp,
                            total_seconds = minutes * 60;

                    if ((tickers.length > 0 && tickers[id] !== dafault_date && tickers[id] !== false)) {
                        $(element).data('TargetDate', tickers[id]);
                    }
                    onTicker['data-' + id] = secs;
                    $('#painel-online-' + id).removeClass('red').removeClass('orange');
                    $('#painel-online-' + id + ' > a[role="addLance"]').removeClass('btn-danger');
                    if (secs > (total_seconds / 2) && secs <= total_seconds && secs % 2 == 0 && secs > 1) {
                        $('#painel-online-' + id).addClass('orange');
                    }

                    if (secs <= (total_seconds / 2) && secs % 2 == 0 && secs > 1) {
                        $('#painel-online-' + id).addClass('red');
                        $('#painel-online-' + id + ' a[role="addLance"]').removeClass('btn-primary').addClass('btn-danger');
                    }
                },
                finish:function(element){
                    var id = $(element).attr('data-lt-id');
                    $('.painel-online-bt[data-lt-id="'+id+'"]').remove();
                    onExpiresCount(element);
                }
            });
    // atualizeLancesPainelAtivos(url_site + 'home/getDataAtivosLances');
    atualizeLancesPainel(url_site + 'home/getDataLoteLances');


    function onExpiresCount(element) {
        var id = $(element).attr('data-id'),
                markup = '<div class="box-encerrado"></div>' +
                '<div class="text-encerrado"><span class="glyphicon glyphicon-ban-circle"></span> Encerrado</div>';

        $('#painel-online-' + id).prepend(markup).delay(15000).fadeOut('1000', function() {
            $(element).remove();
        });
        var id = $(element).attr('data-id'),
                lote = $(element).attr('data-lt-id'),
                leilao = $(element).attr('data-le-id'),
                ativo = $(element).attr('data-at-id'),
                markup = '<div class="box-encerrado"></div>' +
                '<div class="text-encerrado"><span class="glyphicon glyphicon-ban-circle"></span> Encerrado</div>';
        var idata = {
            lote: lote,
            ativo: ativo,
            leilao: leilao
        };
        $.ajax({
            url: url_site + 'home/finishLote',
            dataType: 'json',
            type: 'post',
            data: idata,
            success: function(data) {

                //location.reload();
            }
        });
    }


    $('a[role="addLance"]').click(function(e) {
        e.preventDefault();
        var id = $(this).attr('data-id'),
                objCount = $('.clock-count-down[data-id="' + id + '"]'),
                sd = objCount.attr('data-count'),
                data = sd.split(' '),
                ar_date = data['0'].split('-'),
                ar_hour = data['1'].split(':'),
                sum_m = parseInt(ar_hour['1'], 10) + 3;
        sum_m = sum_m < 10 ? '0' + sum_m : sum_m;

        var hours = [ar_hour['0'], sum_m, ar_hour['2']].join(':'),
                data_c = ar_date.join('-'),
                countdownDate = [data_c, hours].join(' ');
        objCount.attr('data-count', countdownDate);


        var newYear = new Date(),
                minutes_sec = minutes * 60,
                inewYear = new Date(newYear.getFullYear(), newYear.getMonth(), newYear.getDate(), newYear.getHours(), newYear.getMinutes() + 3);


        if (onTicker['painel-data-' + id] < minutes_sec) {
            $('#painel-online-' + id).removeClass('red');
            $('.clock-count-down[data-id="' + id + '"]').countdown('option', {until: +minutes_sec});

        }

    });

    $('.info-painel-online-incremento').tooltip();
    $('#clock-painel-online').clock({"format": "24", "calendar": "false"});

    //panel-history
    function updateHistoryPainel() {

        $('.on-panel-history').each(function() {
            var id = $(this).attr('data-id'), obj = this;
            $.ajax({
                url: '/home/getHistoryAtivo/' + id,
                success: function(html) {
                    $(obj).attr('data-content', '<div class="panel-history">' + html + '</div>');
                    $('.table-history tbody').find('tr').eq(0).addClass('success');
                }
            });

        }).popover({
            html: true

        });

        setTimeout(updateHistoryPainel, 15000);

    }
    updateHistoryPainel();




});
$(function() {
    var totalImages = $(".img-painel").size();
    var imagesLoaded = 0;
    $('body').loadie();
    $(".img-painel").bind("load", function() {
        $(this).show();

        imagesLoaded++;
        $('body').loadie(imagesLoaded / totalImages);
    });
});




